# +_+ coding:utf-8 +_+

import urllib2
import signature
import response

__author__ = 'cz'


class Request(object):
    _host = str()
    _path = str()
    _headers = dict()
    _parameters = dict()

    def __init__(self):
        pass

    def set_host(self, host):
        self._host = host

    def set_path(self, path):
        self._path = path

    def set_headers(self, headers):
        self._headers = headers

    def set_parameters(self, parameters):
        self._parameters = parameters

    def do_request(self):
        sig = signature.calculate(self._parameters)
        split = ""
        param = ""
        for k, v in self._parameters.iteritems():
            param += split + str(k) + "=" + str(v)
            split = "&"

        url = "https://{host}/{path}?{parameters}&sign={sig}"
        url = url.format(host=self._host,
                         path=self._path,
                         parameters=param,
                         sig=sig)
        req = urllib2.Request(url=url, headers=self._headers)
        f = urllib2.urlopen(req)
        rsp = response.Response(f)
        return rsp

